package cn.coldleaf.wiki.controllers;

import cn.coldleaf.wiki.annotations.AuthIgnore;
import cn.coldleaf.wiki.cache.AppCache;
import cn.coldleaf.wiki.pojo.dto.UserLoginDto;
import cn.coldleaf.wiki.pojo.dto.UserModifyPwdDto;
import cn.coldleaf.wiki.pojo.dto.UserQueryDto;
import cn.coldleaf.wiki.pojo.dto.UserSaveDto;
import cn.coldleaf.wiki.pojo.vo.ResponseVo;
import cn.coldleaf.wiki.pojo.vo.UserInfoVo;
import cn.coldleaf.wiki.pojo.vo.UserLoginVo;
import cn.coldleaf.wiki.services.UserService;
import cn.coldleaf.wiki.utils.ErrorCode;
import cn.coldleaf.wiki.utils.StringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户Controller
 * @datetime 2023/10/15 下午1:27
 * @user cold
 **/
@RestController
@Api(tags = "用户服务接口")
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private final UserService userService;
    private final AppCache appCache;

    /**
     * 用户登录
     * @datetime 2023-10-15 13:54:34
     * @user cold
     **/
    @AuthIgnore
    @PostMapping("login")
    @Operation(summary = "用户登录")
    public ResponseVo<UserLoginVo> login(@RequestBody UserLoginDto userLogin){
        String captcha = appCache.remove(userLogin.getCaptchaId(),String.class);
        if (!StringUtil.equals(userLogin.getCaptcha(),captcha)){
            return ResponseVo.error(ErrorCode.CAPTCHA_ERROR);
        }
        UserLoginVo vo = userService.login(userLogin);
        if (vo != null){
            return ResponseVo.success(vo);
        }
        return ResponseVo.error(ErrorCode.LOGIN_ERROR);
    }

    /**
     * 获取用户信息
     * @datetime 2023-10-18 19:22:42
     * @user cold
     **/
    @GetMapping("getInfo")
    @Operation(summary = "获取用户信息")
    public ResponseVo<UserInfoVo> getInfo(UserQueryDto dto){
        UserInfoVo vo = userService.getInfo(dto);
        if (vo != null){
            return ResponseVo.success(vo);
        }
        return ResponseVo.error(ErrorCode.USER_INFO_ERROR);
    }

    /**
     * 创建用户
     * @datetime 2023-10-18 19:22:51
     * @user cold
     **/
    @PostMapping("saveUser")
    @Operation(summary = "创建用户")
    public ResponseVo<UserInfoVo> saveUser(@RequestBody UserSaveDto dto){
        UserInfoVo vo = userService.saveUser(dto);
        if (vo != null){
            return ResponseVo.success(vo);
        }
        return ResponseVo.error(ErrorCode.USER_SAVE_ERROR);
    }

    /**
     * 修改用户
     * @datetime 2023-10-18 19:22:51
     * @user cold
     **/
    @PostMapping("modifyUser")
    @Operation(summary = "修改用户信息")
    public ResponseVo<UserInfoVo> modifyUser(@RequestBody UserSaveDto dto){
        UserInfoVo vo = userService.modifyUser(dto);
        if (vo != null){
            return ResponseVo.success(vo);
        }
        return ResponseVo.error(ErrorCode.USER_MODIFY_ERROR);
    }

    /**
     * 修改用户密码
     * @datetime 2023-10-19 10:14:37
     * @user cold
     **/
    @PostMapping("modifyPwd")
    @Operation(summary = "修改用户密码")
    public ResponseVo<UserInfoVo> modifyPwd(@RequestBody UserModifyPwdDto dto){
        int ret = userService.modifyPwd(dto);
        if (ret > 0){
            return ResponseVo.success(null);
        }
        return ResponseVo.error(ErrorCode.USER_MODIFY_PWD_ERROR);
    }

    /**
     * 重置密码
     * @datetime 2023-10-19 10:14:37
     * @user cold
     **/
    @PostMapping("resetPwd")
    @Operation(summary = "重置用户密码")
    public ResponseVo<UserInfoVo> resetPwd(@RequestBody UserModifyPwdDto dto){
        int ret = userService.resetPwd(dto);
        if (ret > 0){
            return ResponseVo.success(null);
        }
        return ResponseVo.error(ErrorCode.USER_RESET_PWD_ERROR);
    }

    /**
     * 查询用户列表
     * @datetime 2023-10-28 11:52:10
     * @user cold
     **/
    @GetMapping("searchList")
    @Operation(summary = "查询用户列表")
    public ResponseVo<IPage<UserInfoVo>> searchList(UserQueryDto dto){
        IPage<UserInfoVo> vo = userService.searchList(dto);
        return ResponseVo.success(vo);
    }
}
